/**
 *
 */
package gemini.basic.dao.impl;

import gemini.basic.dao.GmnDao;
import gemini.basic.dao.PersonDao;
import gemini.basic.model.AbstractDomain;
import gemini.basic.model.Person;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 *
 */
public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {

	private GmnDao gmnDao;

	public void setGmnDao(GmnDao gmnDao) {
		this.gmnDao = gmnDao;
	}

	@Override
	public Person saveOrUpdate(Person person, boolean flush) {
		Person result = null;
		if (flush) {
			result = (Person) gmnDao.saveOrUpdateAndFlush(person);
		} else {
			result = (Person) gmnDao.saveOrUpdate(person);
		}
		return result;
	}

	@Override
	public Person getById(int id) {
		DetachedCriteria dtCri = DetachedCriteria.forClass(Person.class);

		if (id != AbstractDomain.TRANSIENT_ENTITY_ID) {
			dtCri.add(Restrictions.eq("id", id));
		}

		List<Object> personList = gmnDao.findByCriteria(dtCri);

		Person result = null;
		
		if (personList != null && !personList.isEmpty()) {
			result = (Person) personList.get(0);
		}
		return result;
	}

}
